package com.ziq.zcamp.dialog.share

import android.content.ContentResolver
import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.gmlive.camp.dialog.share.SharePicCacheInterface
import com.ziq.zcamp.R
import com.ziq.zcamp.api.service.ApiUrlHelper
import com.ziq.zcamp.bean.topics.TopicsShareInfo
import com.ziq.zcamp.constant.H5Url
import com.ziq.zcamp.databinding.LayoutSharePicCacheTopicsBinding
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.UserManager
import com.ziq.zcamp.util.ImageUtil

class SharePicCacheLayoutTopics @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr), SharePicCacheInterface {

    var binding : LayoutSharePicCacheTopicsBinding = LayoutSharePicCacheTopicsBinding.inflate(LayoutInflater.from(context), this, true)

    var imageList = mutableListOf<String>()

    var topics : TopicsShareInfo? = null

    private val uidSuffix by lazy{
        val id = UserManager.instance.userInfo.value?.id.toString()
        if (id.isEmpty()){
            ""
        }else{
            "&shareuid=${id}"
        }
    }

    fun setTopicsData(data: TopicsShareInfo?){
        topics = data
    }

    /**
     * 图片加载完成
     */
    override fun checkPictureBitmap(action : ()->Unit){
        val length = imageList.size
        var index = -1
        fun callback(){
            index++
            if(index < length){
                val url = imageList[index]
                ImageUtil.getBitmap(0, url, 0, 0) { _, _ ->
                    callback()
                }
            } else {
                action.invoke()
            }
        }
        callback()
    }

    override fun getMiniProgramPath(): String {
        topics?.let {
            return@getMiniProgramPath "/subpages/community/topic-detail/index?topicId=${topics?.topicId}${uidSuffix}"
        }
        return "/pages/community/index"
    }

    /**
     * 完整卡片
     */
    override fun getTotalLayoutBitmap() : Bitmap?{
        return ImageUtil.convertViewToBitmap(binding.root,
            GlobalContextManager.getResources().getDimensionPixelSize(R.dimen.dimens_dip_375),
            GlobalContextManager.getResources().getDimensionPixelSize(R.dimen.dimens_dip_667))
    }

    /**
     * 小程序分享标题
     */
    override fun getTitleForMiniProgram() : String{
        if(topics != null ){
            return "我发现了有趣的话题#${topics?.topicsName}"
        }
        return ""
    }
    /**
     * 小程序分享卡片封面
     */
    override fun getCoverBitmapForMiniProgram(action : (bitmap : Bitmap?)->Unit){
        var pic = ""
        if(topics != null ){
            if(topics?.cover?.url?.isNotEmpty() == true){
                pic = topics?.cover?.url?:""
            }else{
                pic = "${ContentResolver.SCHEME_ANDROID_RESOURCE}://${resources.getResourcePackageName(R.drawable.bg_topics_share)}/${resources.getResourceTypeName(R.drawable.bg_topics_share)}/${resources.getResourceEntryName(R.drawable.bg_topics_share)}"
            }
        }
        ImageUtil.getBitmap(0, pic, 0, 0) { _, bitmap ->
            action.invoke(bitmap)
        }
    }
    /**
     * 复制链接
     */
    override fun getLinkTxt() : String{
        topics?.let {
            val url = ApiUrlHelper.instance.getUrl("${H5Url.TOPICS_DETAILS.url}?topicId=${topics?.topicId}")
            if (it.userName?.isNotEmpty() == true){
                return@getLinkTxt "${it.userName}正在「觅野CAMP」和露营玩家们讨论：#${it.topicsName}，快来围观~\n $url"
            }else{
                return@getLinkTxt "TA正在「觅野CAMP」和露营玩家们讨论：#${it.topicsName}，快来围观~\n $url"
            }
        }
        return ""
    }

    /**
     * 保存到本地
     */
    override fun onSavePicToLocal(source:String,finish:()->Unit){
        finish()
    }
}